//
//  HNSH_BusinessCircleManager.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/2/27.
//

#import "HNSH_BusinessCircleManager.h"
#import "ST_PageView.h"

#import "HNSH_LocationHelper.h"

#import "HNSH_BusinessCircleSearchViewController.h"
#import "HNSH_BusinessCircleRecordManage.h"

#import "HNSH_AgentAddressView.h"
#import "HNSH_AddressModel.h"

@interface HNSH_BusinessCircleManager ()<HNSH_AgentAddressViewDelegate>

/* addressLabel */
@property (nonatomic, strong) UILabel *addressLabel;

/** */
@property (nonatomic, strong)HNSH_AddressModel * addressModel;

@end

@implementation HNSH_BusinessCircleManager

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor appWhiteLabelColor];
    
    [self showNavTitle:@"商圈" showLeftItem:NO];
    
    //默认拿全国数据
    [[NSUserDefaults standardUserDefaults] setInteger:1 forKey:@"KGetAllCityData"];

    //移除之前选择
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"KSelectLocationCode"];

    //定位
    WeakSelf(weakSelf);
    [[HNSH_LocationHelper share] getCurrentLocationWithBlock:^(NSString * _Nonnull address, NSString * _Nonnull city, NSString * _Nonnull adcode, double longitude, double latitude) {
        weakSelf.addressLabel.text = city;
        NSDictionary *dict = @{@"region_code":adcode,
                               @"latitude":@(latitude),
                               @"longitude":@(longitude)
        };

        [[NSUserDefaults standardUserDefaults] setObject:dict forKey:@"KCurrentLocation"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"KBusinessCircleReload" object:self userInfo:nil];
    }];

    [self loadBusinessCircleNavTitle];

    [self configSearch];
}

- (void)configSearch {
    UIImageView *bgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kNavBarAndStatusBarHeight+62)];
    bgView.image = [UIImage imageNamed:@"b_circle_top_bg"];
    [self.view addSubview:bgView];
    
    UIImageView *titleIcon = [[UIImageView alloc]initWithFrame:CGRectMake(12, kStatusBarHeight+12, 48, 24)];
    titleIcon.image = [UIImage imageNamed:@"b_circle_top_title"];
    [self.view addSubview:titleIcon];
    
    UIImageView *addressIcon = [[UIImageView alloc]initWithFrame:CGRectMake(72, 18+kStatusBarHeight, 16, 16)];
    addressIcon.image = [UIImage imageNamed:@"b_circle_top_address_icon"];
    [self.view addSubview:addressIcon];
    
    UILabel *addressLabel = [[UILabel alloc]init];
    addressLabel.text = @"全国";
    addressLabel.textColor = [UIColor appWhiteLabelColor];
    addressLabel.font = [UIFont boldSystemFontOfSize:14];
    [self.view addSubview:addressLabel];
    self.addressLabel = addressLabel;
    [addressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(addressIcon.mas_right).offset(2);
        make.centerY.equalTo(addressIcon.mas_centerY);
        make.height.mas_equalTo(32);
    }];
    
    WeakSelf(weakSelf);
    addressLabel.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithActionBlock:^(id  _Nonnull sender) {
        [weakSelf chooseAddress];
    }];
    [addressLabel addGestureRecognizer:tap];
    
    
    UIImageView *downIcon = [[UIImageView alloc]init];
    downIcon.image = [UIImage imageNamed:@"b_circle_top_address_down"];
    [self.view addSubview:downIcon];
    [downIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(addressLabel.mas_right).offset(0);
        make.centerY.equalTo(addressLabel.mas_centerY);
        make.height.mas_equalTo(16);
        make.height.mas_equalTo(16);
    }];
    
    UIView *searchBgView = [[UIView alloc]init];
    searchBgView.backgroundColor = [UIColor appWhiteLabelColor];
    [self.view addSubview:searchBgView];
    searchBgView.layer.cornerRadius = 16.0f;
    searchBgView.layer.masksToBounds = YES;
    [searchBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(12);
        make.height.mas_equalTo(32);
        make.top.mas_equalTo(kNavBarAndStatusBarHeight+6);
        make.right.mas_equalTo(-12);
    }];
    
    UILabel *searchLabel = [[UILabel alloc]init];
    searchLabel.text = @"搜索附近商家";
    searchLabel.textColor = [UIColor appGrayLabelColor];
    searchLabel.font = [UIFont systemFontOfSize:14];
    [searchBgView addSubview:searchLabel];
    [searchLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(44);
        make.top.bottom.mas_equalTo(0);
    }];
    
    UIImageView *searchIcon = [[UIImageView alloc]init];
    searchIcon.image = [UIImage imageNamed:@"b_circle_top_icon_search"];
    [searchBgView addSubview:searchIcon];
    [searchIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.centerY.equalTo(searchBgView.mas_centerY);
        make.width.mas_equalTo(16);
        make.height.mas_equalTo(16);
    }];
    
    
    UILabel *searchLabel1 = [[UILabel alloc]init];
    searchLabel1.text = @"搜索";
    searchLabel1.textColor = [UIColor appGreenColor];
    searchLabel1.font = [UIFont systemFontOfSize:14];
    [searchBgView addSubview:searchLabel1];
    [searchLabel1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-16);
        make.top.bottom.mas_equalTo(0);
    }];
    
    searchBgView.userInteractionEnabled = YES;
    UITapGestureRecognizer *searchTap = [[UITapGestureRecognizer alloc]initWithActionBlock:^(id  _Nonnull sender) {
        HNSH_BusinessCircleSearchViewController *vc = [[HNSH_BusinessCircleSearchViewController alloc]init];
        [weakSelf.navigationController pushViewController:vc animated:YES];
    }];
    [searchBgView addGestureRecognizer:searchTap];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:@"消费记录" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor appWhiteLabelColor] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont boldSystemFontOfSize:14];
    button.frame = CGRectMake(kScreenWidth-96, kStatusBarHeight, 80, 44);
    [self.view addSubview:button];
    [[button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        HNSH_BusinessCircleRecordManage *vc = [[HNSH_BusinessCircleRecordManage alloc]init];
        [weakSelf.navigationController pushViewController:vc animated:YES];
    }];
}

- (void)configPageWithAry:(NSArray *)ary {
    NSMutableArray *vcs = [NSMutableArray arrayWithCapacity:ary.count];
    NSMutableArray *titles = [NSMutableArray arrayWithCapacity:ary.count];
    NSMutableArray *ids = [NSMutableArray arrayWithCapacity:ary.count];
    for (NSDictionary *dic in ary) {
        [vcs addObject:@"HNSH_BusinessCircleViewController"];
        [titles addObject:[NSString stringWithFormat:@"%@",dic[@"name"]]];
        [ids addObject:[NSString stringWithFormat:@"%@",dic[@"id"]]];
    }
    
    ST_PageView *pageView = [[ST_PageView alloc]initWithFrame:CGRectMake(0, kNavBarAndStatusBarHeight+62, kScreenWidth, kScreenHeight-kNavBarAndStatusBarHeight-62) withTitles:titles withViewControllers:vcs withParameters:ids];
    pageView.isAnimated = NO;
    pageView.selectedColor = [UIColor appBlackLabelColor];
    pageView.unselectedColor = [UIColor appGrayLabelColor];
    pageView.leftSpace = pageView.rightSpace = 20;
    pageView.isShowTopTabBottomLine = NO;
    pageView.isAverage = YES;
    pageView.font = [UIFont systemFontOfSize:16];
    pageView.scrollView.backgroundColor = [UIColor appWhiteLabelColor];
    [self.view addSubview:pageView];
}

- (void)loadBusinessCircleNavTitle {
    [HNSH_ShopNetServer requestBusinessCircleNavTitleSuccess:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.code == 0) {
            [self configPageWithAry:responseObject.resultset];
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];
}

- (HNSH_AddressModel *)addressModel{
    if (!_addressModel) {
        _addressModel = [HNSH_AddressModel new];
    }
    return _addressModel;
}

-(void)cancelBtnClick:(NSString *)titleAddress titleID:(NSString *)titleID {
    NSArray *titleAry = [titleAddress componentsSeparatedByString:@"/"];
    NSLog(@"ary ==> %@  %@",titleAry,titleAry.lastObject);
    NSString *name = titleAry.lastObject;
    
    NSArray *idAry = [titleID componentsSeparatedByString:@"/"];
    NSLog(@"ary ==> %@  %@",idAry,idAry.lastObject);
    NSString *region_code = idAry.lastObject;
    
    [[NSUserDefaults standardUserDefaults] setObject:region_code forKey:@"KSelectLocationCode"];
    [[NSUserDefaults standardUserDefaults] setObject:name forKey:@"KSelectLocationCodeName"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    self.addressLabel.text = name;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"KBusinessCircleSelectCityReload" object:self userInfo:nil];
}

//MARK: 选择地区
- (void)chooseAddress{
    [self.view endEditing:YES];
    HNSH_AgentAddressView *view = [[HNSH_AgentAddressView alloc]init];
    view.title = @"选择地址";
    view.delegate = self;
    view.defaultHeight = 350;
    view.titleScrollViewH = 37;
    if (view.titleIDMarr.count > 0) {
        view.isChangeAddress = true;
    }
    else{
        view.isChangeAddress = false;
    }
   
    [self.view addSubview:[view initAddressView]];
    [view addAnimate];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
